package com.lx.mq.mqserver.datacenter;

import com.lx.mq.MqApplication;
import com.lx.mq.mqserver.core.Binding;
import com.lx.mq.mqserver.core.Exchange;
import com.lx.mq.mqserver.core.ExchangeType;
import com.lx.mq.mqserver.core.MSGQueue;
import com.lx.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */

/**
 * 数据库对交换机、队列、绑定的管理
 */
public class DataBaseManager {
    private MetaMapper metaMapper;
    public void init(){
        if(!checkDBExists()){
            metaMapper= MqApplication.context.getBean(MetaMapper.class);

            //数据库不存在，就先进行建库建表操作
            //先创建一个data目录
            File dataDir=new File("./data");
            dataDir.mkdir();
            //创建数据表
            createTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager]:数据库初始化完成");
        }else{
            System.out.println("[DataBaseManager]:数据库已经存在");
        }
    }
    public boolean checkDBExists(){
        File file=new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }
    //这个方法用来建表
    //数据库不需要手动创建，首次执行这里的数据库操作的时候，就自动创建好了（Mybatis完成的）
    public void createTable(){
        metaMapper.createBindingTable();
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        System.out.println("【DatabaseManager】:创建表完成");
    }
    //给数据库表中添加默认的数据
    //RabbitMQ中有这样的设定：带有一个 匿名 的交换机，类型是DIRECT
    public void createDefaultData(){
        // 构造一个默认的交换机.
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成!");

    }
    public void deleteDB(){
        File file=new File("./data/meta.db");
        boolean ret=file.delete();
        if(ret){
            System.out.println("[DatabaseManager]:删除数据库文件成功");
        }else{
            System.out.println("[DatabaseManager]:删除数据库文件失败");
        }
        File dataDir=new File("./data");
        //此处删除目录的时候需要保证目录是空的
        //如果上面删除数据库文件失败的话，目录就不为空，此时删除目录就会失败
        ret=dataDir.delete();
        if(ret){
            System.out.println("[DatabaseManager]:删除数据库目录成功");
        }else{
            System.out.println("[DatabaseManager]:删除数据库目录失败");
        }
    }
    // 把其他的数据库的操作, 也在这个类中封装一下.
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
}
